/**
 * 2018年12月20日下午10:55:57
 * @author 谢进财
 */
package com.baihang.framework.kit;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.multipart.MultipartFile;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.Session;

/**
 * 
 *
 */
public class SSHKit {
	private static SCPClient scp;
	private static Connection conn;
	public static String SSHSERVERIP;//服务器ip
	public static String SSHUSERNAME;//用户名
	public static String SSHPASSWORD;//密码
	
	/**
	 * 获取远程连接
	 * @param serverIp
	 * @param username
	 * @param password
	 */
	public static void getConnect(String serverIp, String username, String password) {
		try {
			conn = new Connection(serverIp);
			conn.connect();
			conn.authenticateWithPassword(username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取客户端
	 */
	public static void getConnAndScp() {
		SSHKit.getConnect(SSHSERVERIP, SSHUSERNAME, SSHPASSWORD);
		scp = getSCPClient();
	}

	public static SCPClient getSCPClient() {
		return scp = new SCPClient(conn);
	}
	
	/**
	 * 
	 * @param data //文件流
	 * @param fileName //文件名
	 * @param remoteTargetDirectory //远程目录
	 * @return
	 */
	public static boolean upload(byte[] data, String fileName, String remoteTargetDirectory) {
		boolean flag = false;
		try {
			SSHKit.getConnAndScp();
			// ifDirExist();            
			scp.put(data, fileName, remoteTargetDirectory, "0777");
			flag = true;
			conn.close();
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 
	 * @param remoteTargetDirectory
	 * @param localDirectory
	 */
	public static void download(String remoteTargetDirectory, String localDirectory) {
		try {
			getConnAndScp();
			scp.get(remoteTargetDirectory, localDirectory);
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除
	 * @param fileName
	 */
	public static void remove(String path,String fileName) {
		try {
			SSHKit.getConnect(SSHSERVERIP, SSHUSERNAME, SSHPASSWORD);
			SFTPv3Client sftpClient = new SFTPv3Client(conn);
			sftpClient.rm(path + fileName);
			sftpClient.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获得上传文件的本地路径和文件名称
	 * @param file
	 * @return
	 */
	public static Map<String, String> getAbsolutePath(MultipartFile file) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			// 创建临时文件
			File f = File.createTempFile("", ".jpg");
			// 将文件转为file文件
			file.transferTo(f);
			// 获取本地路径
			String filePath = f.getAbsolutePath();
			// 获取文件名称
			String newFileName = f.getName();
			map.put("filePath", filePath);
			map.put("fileName", newFileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * File的InputStream 转为byte[]
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public static byte[] toByteArray(InputStream in) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] bufferValue = new byte[in.available()];
		byte[] buffer = new byte[1024 * 4];
		int n = 0;
		while ((n = in.read(buffer)) != -1) {
			out.write(buffer, 0, n);
		}
		bufferValue = out.toByteArray();
		out.close();
		return bufferValue;
	}
	/**
	 * 创建目录
	 * @param remoteTargetDir
	 */
	public static void makeDir(String remoteTargetDir) {
		try {
			SSHKit.getConnect(SSHSERVERIP, SSHUSERNAME, SSHPASSWORD);
			SFTPv3Client sftpClient = new SFTPv3Client(conn);
			sftpClient.mkdir(remoteTargetDir, 0777);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 判断目录是否存在
	 * @param remoteTargetDir
	 */
	public static void ifDirExist(String remoteTargetDir) {
		try {
			String cmd = String.format("[ -d %s ]", remoteTargetDir);
			Session session = conn.openSession();// 打开一个会话
			session.execCommand(cmd);// 执行命令
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception, IOException {
		File file = new File("E:\\Test\\logo.png");
		byte[] fileByte = SSHKit.toByteArray(new FileInputStream(file));
		SSHKit.SSHSERVERIP="47.99.137.243";
		SSHKit.SSHUSERNAME="root";
		SSHKit.SSHPASSWORD="Dskj2018";
		
		SSHKit.upload(fileByte, "logo.png", "/usr/share/nginx/html/UpLoad/fy");
	}
}
